package com.yalemang.uindicator;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

/**
 * 万能指示器
 * 仅用于指示器
 */
public class UniversalIndicator extends FrameLayout {

    protected View contentView;
    protected RecyclerView rvIndicator;
    //默认是横向的
    protected Orientation orientation = Orientation.HORIZONTAL;
    protected Adapter adapter;

    public int getSelectPosition() {
        return adapter.getSelectPosition();
    }

    public void setSelectPosition(int position) {
        int copySelectPosition = adapter.selectPosition;
        adapter.setSelectPosition(position);
        Log.d("Ellen2020",""+(copySelectPosition <= position));
        int locationPosition = adapter.selectPosition + adapter.locationDeviation(copySelectPosition <= position);
        rvIndicator.scrollToPosition(locationPosition);
    }

    public void addOnItemClickListener(OnItemClickListener onItemClickListener) {
        adapter.onItemClickListeners.add(onItemClickListener);
    }

    public void removeItemClickListener(OnItemClickListener onItemClickListener) {
        adapter.onItemClickListeners.remove(onItemClickListener);
    }

    public void addOnSelectListener(OnSelectListener onSelectListener) {
        adapter.onSelectListeners.add(onSelectListener);
    }

    public void removeOnSelectListener(OnSelectListener onSelectListener) {
        adapter.onSelectListeners.remove(onSelectListener);
    }

    public Adapter getAdapter() {
        return adapter;
    }

    public void setAdapter(Adapter adapter) {
        this.adapter = adapter;
        this.adapter.onAttach(this);
        //设置指示器方向
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        if (orientation == Orientation.HORIZONTAL) {
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        } else {
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        }
        rvIndicator.setLayoutManager(layoutManager);
        rvIndicator.setAdapter(adapter);
    }

    public Orientation getOrientation() {
        return orientation;
    }

    public void setOrientation(Orientation orientation) {
        this.orientation = orientation;
    }

    public UniversalIndicator(@NonNull Context context) {
        super(context, null);
    }

    public UniversalIndicator(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    public UniversalIndicator(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        //解析自定义属性
        //绑定布局
        contentView = LayoutInflater.from(getContext()).inflate(R.layout.view_u_indicator, this);
        rvIndicator = contentView.findViewById(R.id.rv_indicator);
    }

    public void bindViewPager(ViewPager2 viewPager2) {
        //如果是0就会出现问题，不知道怎么回事
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {

            boolean isInit = false;

            @Override
            public void onPageSelected(int position) {
                if (!(position == 0 && !isInit)) {
                    setSelectPosition(position);
                    isInit = true;
                }
            }
        });
        //点击之后不响应状态变化，避免重复响应
        adapter.isClickResponse = false;
        addOnSelectListener(new OnSelectListener() {
            @Override
            public void onSelectPosition(int oldPosition, int newPosition) {
                viewPager2.setCurrentItem(newPosition);
            }

            @Override
            public void onReSelectPosition(int position) {
                //因为之前屏蔽了复选状态变化，所以这里添加上
                setSelectPosition(position);
            }

            @Override
            public void onUnStatusClick(int position) {

            }
        });
    }

    public void bindViewPager(ViewPager viewPager) {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

            @Override
            public void onPageSelected(int position) {
                setSelectPosition(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {}
        });
        //点击之后不响应状态变化，避免重复响应
        adapter.isClickResponse = false;
        addOnSelectListener(new OnSelectListener() {
            @Override
            public void onSelectPosition(int oldPosition, int newPosition) {
                viewPager.setCurrentItem(newPosition);
            }

            @Override
            public void onReSelectPosition(int position) {
                //因为之前屏蔽了复选状态变化，所以这里添加上
                setSelectPosition(position);
            }

            @Override
            public void onUnStatusClick(int position) {

            }
        });
    }

    //方向
    public enum Orientation {
        HORIZONTAL, VERTICAL
    }

    public interface OnItemClickListener {
        void itemClick(View v, int position);
    }

    public abstract static class OnSelectListener {
        public abstract void onSelectPosition(int oldPosition, int newPosition);

        public abstract void onReSelectPosition(int position);

        public void onUnStatusClick(int position) {

        }
    }

}
